using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebSvrUpdaterCore.Models;
using WebUpdaterAdminCore.Data;

namespace WebUpdaterAdminCore.SocketServer
{
    public class ClientCommandHandle
    {
        private NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
        private UpdaterSocketSession _session;
        private ApplicationDbContext _dbContext;

        public ClientCommandHandle(UpdaterSocketSession session,ApplicationDbContext context)
        {
            _session = session;
            _dbContext = context;
        }

        public void HandleCheckUpdate(MsgPacket packet)
        {
            CheckUpdateRespone respone = packet.ConvertBody<CheckUpdateRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.CheckUpdateReply, respone);
        }

        public async void HandleCheckUpdateAsync(MsgPacket packet)
        {
            CheckUpdateRespone respone = packet.ConvertBody<CheckUpdateRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.CheckUpdateReply, respone);

        }

        public void HandleGetVersion(MsgPacket packet)
        {
            GetVersionRespone respone = packet.ConvertBody<GetVersionRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.GetVersionReply, respone);
        }

        public async void HandleGetVersionAsync(MsgPacket packet)
        {
            GetVersionRespone respone = packet.ConvertBody<GetVersionRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.GetVersionReply, respone);
        }

        public void HandleWebSiteInfo(MsgPacket packet)
        {
            WebSiteInfoRespone respone = packet.ConvertBody<WebSiteInfoRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.WebSiteInfoReply,respone);
            //Console.WriteLine("获取站在信息结果"+packet.GetTextBody());
        }

        public async void HandleWebSiteInfoAsync(MsgPacket packet)
        {
            WebSiteInfoRespone respone = packet.ConvertBody<WebSiteInfoRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.WebSiteInfoReply,respone);
            //Console.WriteLine("获取站在信息结果" + packet.GetTextBody());
        }

        public void HandleWebSiteAdd(MsgPacket packet)
        {
            WebSiteAddRespone respone = packet.ConvertBody<WebSiteAddRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.WebSiteAddReply, respone);
        }

        public async void HandleWebSiteAddAsync(MsgPacket packet)
        {
            WebSiteAddRespone respone = packet.ConvertBody<WebSiteAddRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.WebSiteAddReply, respone);
        }

        public void HandleWebSiteEdit(MsgPacket packet)
        {
            WebSiteEditRespone respone = packet.ConvertBody<WebSiteEditRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.WebSiteEditReply, respone);
        }

        public async void HandleWebSiteEditAsync(MsgPacket packet)
        {
            WebSiteEditRespone respone = packet.ConvertBody<WebSiteEditRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.WebSiteEditReply, respone);
        }


        public void HandleWebSiteDelete(MsgPacket packet)
        {
            WebSiteDeleteRespone respone = packet.ConvertBody<WebSiteDeleteRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.WebSiteDeleteReply, respone);
        }

        public async void HandleWebSiteDeleteAsync(MsgPacket packet)
        {
            WebSiteDeleteRespone respone = packet.ConvertBody<WebSiteDeleteRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.WebSiteDeleteReply, respone);
        }

        public void HandleAppPoolInfo(MsgPacket packet)
        {
            AppPoolInfoRespone respone = packet.ConvertBody<AppPoolInfoRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.AppPoolInfoReply, respone);
        }

        public async void HandleAppPoolInfoAsync(MsgPacket packet)
        {
            AppPoolInfoRespone respone = packet.ConvertBody<AppPoolInfoRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.AppPoolInfoReply, respone);
        }

        public void HandleAppPoolAdd(MsgPacket packet)
        {
            AppPoolAddRespone respone = packet.ConvertBody<AppPoolAddRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.AppPoolAddReply, respone);
        }

        public async void HandleAppPoolAddAsync(MsgPacket packet)
        {
            AppPoolAddRespone respone = packet.ConvertBody<AppPoolAddRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.AppPoolAddReply, respone);
        }

        public void HandleAppPoolEdit(MsgPacket packet)
        {
            AppPoolEditRespone respone = packet.ConvertBody<AppPoolEditRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.AppPoolEditReply, respone);
        }

        public async void HandleAppPoolEditAsync(MsgPacket packet)
        {
            AppPoolEditRespone respone = packet.ConvertBody<AppPoolEditRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.AppPoolEditReply, respone);

        }

        public void HandleAppPoolDelete(MsgPacket packet)
        {
            AppPoolDeleteRespone respone = packet.ConvertBody<AppPoolDeleteRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.AppPoolDeleteReply, respone);
        }

        public async void HandleAppPoolDeleteAsync(MsgPacket packet)
        {
            AppPoolDeleteRespone respone = packet.ConvertBody<AppPoolDeleteRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.AppPoolDeleteReply, respone);
        }

        public void HandleDownload(MsgPacket packet)
        {
            DownloadRespone respone = packet.ConvertBody<DownloadRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.DownLoadReply, respone);
        }

        public async void HandleDownloadAsync(MsgPacket packet)
        {
            DownloadRespone respone = packet.ConvertBody<DownloadRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.DownLoadReply, respone);
        }

        public void HandleUpload(MsgPacket packet)
        {
            UploadRespone respone = packet.ConvertBody<UploadRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.UploadReply, respone);
        }

        public async void HandleUploadAsync(MsgPacket packet)
        {
            UploadRespone respone = packet.ConvertBody<UploadRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.UploadReply,respone);
        }

        public  void HandleCheckSiteName(MsgPacket packet)
        {
            CheckSiteNameRespone respone = packet.ConvertBody<CheckSiteNameRespone>();
            UpdateSendHistory(packet.ReqesutId, MsgTypes.CheckUpdateReply, respone);
        }

        public async void HandleCheckSiteNameAsync(MsgPacket packet)
        {
            CheckSiteNameRespone respone = packet.ConvertBody<CheckSiteNameRespone>();
            await UpdateSendHistoryAsync(packet.ReqesutId, MsgTypes.CheckUpdateReply, respone);
        }

        private bool UpdateSendHistory(string id, string msgType, BaseRespone data)
        {
            bool bRet = false;
            try
            {
                UpdaterSocketServerService.AddReceiveMsg(new Models.MsgReceiveModel
                {
                    RequestId = id,
                    ReceiveMsgType = msgType,
                    ClientId = _session.ClientId,
                    SessionId = _session.SessionID,
                    ReceiveData = data
                });
                if (_dbContext != null)
                {
                    //var id = "";
                    var model =  _dbContext.SendHistories.Find(id);
                    if (model == null)
                    {
                        _logger.Warn($"id={id},找不到记录");
                        return false;
                    }
                    model.ReceiveTime = DateTime.Now;
                    model.ReceiveType = msgType;
                    model.ReceiveUser = _session.ClientId;
                    //model.ReceiveContent
                    _dbContext.SendHistories.Update(model);
                    var count =  _dbContext.SaveChanges();
                    bRet = count > 0;
                }
                else
                {
                    _logger.Error("UpdateMsgHistory无法连接数据库");
                    bRet = false;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "UpdateMsgHistory出现异常");
                bRet = false;
            }

            return bRet;
        }

        private async Task<bool> UpdateSendHistoryAsync(string id,string msgType, BaseRespone data)
        {
            bool bRet = false;
            try
            {
                UpdaterSocketServerService.AddReceiveMsg(new Models.MsgReceiveModel
                {
                    RequestId = id,
                    ClientId = _session.ClientId,
                    SessionId = _session.SessionID,
                    ReceiveMsgType = msgType,
                    ReceiveData = data
                });
                if (_dbContext != null)
                {
                    var model = await _dbContext.SendHistories.FindAsync(id);
                    if(model == null)
                    {
                        _logger.Warn($"id={id},找不到记录");
                        return false;
                    }
                    model.ReceiveTime = DateTime.Now;
                    model.ReceiveType = msgType;
                    model.ReceiveUser = _session.ClientId;
                    model.ReceiveContent = data.ResultMsg;
                    _dbContext.SendHistories.Update(model);
                    var count = await _dbContext.SaveChangesAsync();
                    bRet = count > 0;
                }
                else
                {
                    _logger.Error("UpdateMsgHistory无法连接数据库");
                    bRet = false;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "UpdateMsgHistory出现异常");
                bRet = false;
            }

            return bRet;
        }
    }
}
